// wx.config({
//  debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
//  appId: '', // 必填，公众号的唯一标识
//  timestamp: , // 必填，生成签名的时间戳
//  nonceStr: '', // 必填，生成签名的随机串
//  signature: '',// 必填，签名，见附录1
//  jsApiList: [] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
// });
import * as config from '../config.js';
import Vue from 'vue';
import {shareMap} from '../filters';

export function getKey(_orderNo, _payChinnel = 'WXPAY_PUB') {
  let $data = {
    orderNo: _orderNo,
    payChannel: _payChinnel
  };
  return Vue.http.post(config.config.weixin.getkey, $data);
}

export function getSign(_url) {
  return Vue.http.get(config.config.weixin.getSign + '?url=' + _url);
}

export function getWebOAuthUrl(_appid = config.configInfo.WX.appid, _redirectUri = window.location.href, _redirectServerUri = config.configInfo.WX._redirectServerUri, _state = config.configInfo.WX.state || '') {
  return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${_appid}&redirect_uri=${encodeURIComponent(_redirectServerUri)}&response_type=code&scope=snsapi_userinfo&state=${encodeURIComponent(_state)}&connect_redirect=1#wechat_redirect`;
}

export function setWx(currentRouter) {
  window.setWeixi();
  window.wxData.timer = setInterval(() => {
    console.log('重新设置');
    window.wx.config(window.wxData.config);
  }, 1000);
  window.wx.error((err) => {
    console.log(err);
  });
  window.wx.ready(() => {
    window.wxData.ready = true;
    clearInterval(window.wxData.timer);
    window.wx.getNetworkType({
      success: function (res) {
        window.support.networkType = res.networkType; // 返回网络类型2g，3g，4g，wifi
        console.log(window.support.networkType);
      }
    });
    // 当前router如果不允许分享，则隐藏分享按钮
    if (!currentRouter.share) {
      window.wx.hideMenuItems({
        menuList: [
          'menuItem:openWithSafari',
          'menuItem:share:email',
          'menuItem:openWithQQBrowser',
          'menuItem:share:email',
          'menuItem:share:qq',
          'menuItem:share:QZone',
          'menuItem:copyUrl',
          'menuItem:share:appMessage',
          'menuItem:share:timeline'
        ]
      });
      return;
    }
    if (!currentRouter.shareConfig) {
      // 当前router如果允许分享，如果非自定义的则在这里处理，否则在具体页面里面处理
      let shareData = shareMap(currentRouter.name) || shareMap('defaults');
      window.wx.onMenuShareTimeline(shareData);
      window.wx.onMenuShareAppMessage(shareData);
      return;
    }
  });
}
